<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2019/4/8
 * Time: 17:48
 */

use \waf\Waf;
use \waf\log\WLog;

class MMonitor
{
    /**
     * 根据server_name获取，当前所有的module list
     *
     * @param string $server_name
     * @param string $module_name
     * @param string $type_path
     * @return array
     */
    public static function get_server_module_list($server_name = '', $module_name = '', $type_path = '')
    {
        WLog::logInfo("in. server_name=$server_name, module_name=$module_name", __FILE__, __LINE__, __FUNCTION__);
        $arrRet = array();

        if($server_name)
        {
            $file_path  = Waf::getConfig('monitor', 'log_path').$type_path.'/'.$server_name;
            WLog::logInfo("file_path = $file_path", __FILE__, __LINE__, __FUNCTION__);

            if(is_dir($file_path))
            {
                foreach(glob($file_path."/*", GLOB_ONLYDIR) as $module_file)
                {
                    $tmp = explode("/", $module_file);
                    $module = end($tmp);
                    $arrRet[$module] = array();

                    if($module_name == $module)
                    {
                        $st_dir = $file_path.'/'.$module_name.'/';
                        WLog::logInfo("st_dir = $st_dir", __FILE__, __LINE__, __FUNCTION__);

                        foreach(glob($st_dir."*") as $file)
                        {
                            $tmp = explode("/", $file);
                            $interface = end($tmp);
                            $arrRet[$module][] = $interface;
                        }
                    }
                }
            }
        }

        return $arrRet;
    }

    /**
     * 获取monitor count数据
     *
     * @param string $server_name
     * @param string $module_name
     * @param string $interface_name
     * @param string $date_time
     * @return array
     */
    public static function getMonitorCountData($server_name = '', $module_name = '', $interface_name = '', $date_time = '')
    {
        $arrRet = array();

        if($server_name && $module_name && $interface_name && $date_time)
        {
            $log_file  = Waf::getConfig('monitor', 'log_path').Waf::getConfig('monitor', 'count_log_path').'/'.$server_name.'/'.$module_name.'/'.$interface_name.'/'.$date_time.'.log';
            WLog::logInfo("log_file = $log_file", __FILE__, __LINE__, __FUNCTION__);

            $handle = @fopen($log_file, 'r');
            if(!$handle)
            {
                return $arrRet;
            }

            $arrT = array();
            while(!feof($handle))
            {
                $line = fgets($handle, 4096);
                if($line)
                {
                    $explode = explode("|", $line);
                    if(count($explode) < 2)
                    {
                        continue;
                    }
                    list($date_time, $count) = $explode;
                    $time = strtotime($date_time);
                    WLog::logDebug("date_time=$date_time, count=$count, explode = ".WLog::toJson($explode), __FILE__, __LINE__, __FUNCTION__);

                    if($time)
                    {
                        $time = ceil($time/300)*300;
                        if(!isset($arrRet[$time]))
                        {
                            $arrRet[$time] = 0;
                        }
                        $arrRet[$time] += $count;
                    }
                } // end if
            } // end while

            fclose($handle);
        }

        return $arrRet;
    }

    /**
     * 获取monitor interface数据
     *
     * @param string $server_name
     * @param string $module_name
     * @param string $interface_name
     * @param string $date_time
     * @return array
     */
    public static function getMonitorInterfaceData($server_name = '', $module_name = '', $interface_name = '', $date_time = '')
    {
        $arrRet = array();

        if($server_name && $module_name && $interface_name && $date_time)
        {
            $log_file  = Waf::getConfig('monitor', 'log_path').Waf::getConfig('monitor', 'interface_log_path').'/'.$server_name.'/'.$module_name.'/'.$interface_name.'/'.$date_time.'.log';
            WLog::logInfo("log_file = $log_file", __FILE__, __LINE__, __FUNCTION__);

            $handle = @fopen($log_file, 'r');
            if(!$handle)
            {
                return $arrRet;
            }

            while(!feof($handle))
            {
                $line = fgets($handle, 4096);
                if($line)
                {
                    $explode = explode("|", $line);
                    if(count($explode) < 4)
                    {
                        continue;
                    }
                    list($date_time, $suc_count, $suc_cost_time, $fail_count, $fail_cost_time) = $explode;
                    $time = strtotime($date_time);
                    WLog::logDebug("date_time=$date_time, suc_count=$suc_count, suc_cost_time=$suc_cost_time, fail_count=$fail_count, fail_cost_time=$fail_cost_time, explode = ".WLog::toJson($explode), __FILE__, __LINE__, __FUNCTION__);

                    if($time)
                    {
                        $time = ceil($time/300)*300;
                        if(!isset($arrRet[$time]))
                        {
                            $arrRet[$time] = array();
                        }
                        $arrRet[$time]['suc_count']         += $suc_count;
                        $arrRet[$time]['suc_cost_time']     += $suc_cost_time;
                        $arrRet[$time]['fail_count']        += $fail_count;
                        $arrRet[$time]['fail_cost_time']    += $fail_cost_time;
                    }
                } // end if
            } // end while

            fclose($handle);
        }

        return $arrRet;
    }
}